import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login.vue'
import Home from '@/views/Home.vue'

Vue.use(VueRouter)

const routes = [
  { path: '', redirect: '/home' },
  {
    path: '/login',
    component: Login
  },
  // 访问后台时重定向到欢迎
  { path: '/home', redirect: '/home/welcom' },
  {
    path: '/home',
    component: Home,
    meta: { name: '首页' },
    children: [
      // 右侧欢迎页路由
      {
        path: 'welcom',
        component: () => import('@/views/rightView/Welcom.vue'),
        meta: { name: '首页' }
      },
      {
        path: 'usermenu',
        name: 'usermenu',
        meta: {
          name: '用户管理'
        },
        component: () => import('@/comment/HomeRight.vue'),
        redirect: '/home/users',
        children: [
          // 右侧用户页
          {
            path: '/home/users',
            component: () => import('@/views/rightView/users/User.vue'),
            meta: {
              name: '用户列表'
            }
          }
        ]
      },
      {
        path: 'powermenu',
        name: 'powermenu',
        meta: {
          name: '权限管理'
        },
        component: () => import('@/comment/HomeRight.vue'),
        redirect: '/home/rights',
        children: [
          // 右侧权限列表页
          {
            path: '/home/rights',
            component: () => import('@/views/rightView/power/Rights.vue'),
            meta: {
              name: '权限列表'
            }
          },
          // 右侧角色管理页
          {
            path: '/home/roles',
            component: () => import('@/views/rightView/power/Roles.vue'),
            meta: {
              name: '角色列表'
            }
          }
        ]
      },
      // 商品管理
      {
        path: 'goodmenu',
        name: 'goodmenu',
        meta: {
          name: '商品管理'
        },
        component: () => import('@/comment/HomeRight.vue'),
        redirect: '/home/goods',
        children: [
          // 商品分类
          {
            path: '/home/categories',
            component: () => import('@/views/rightView/goods/Cate.vue'),
            meta: {
              name: '商品分类'
            }

          },
          // 分类参数
          {
            path: '/home/params',
            component: () => import('@/views/rightView/goods/Params.vue'),
            meta: {
              name: '分类参数'
            }
          },
          // 商品列表
          {
            path: '/home/goods',
            component: () => import('@/views/rightView/goods/List.vue'),
            meta: {
              name: '商品列表'
            },
            children: [
              {
                path: '/home/goods/add',
                component: () => import('@/views/rightView/goods/GoodAdd.vue'),
                meta: {
                  name: '添加商品'
                }
              },
              {
                path: '/home/goods/put',
                component: () => import('@/views/rightView/goods/GoodAdd.vue'),
                meta: {
                  name: '修改商品'
                }
              }
            ]
          }
        ]
      },
      // 订单管理
      {
        path: 'ordermenu',
        name: 'ordermenu',
        meta: {
          name: '订单管理'
        },
        component: () => import('@/comment/HomeRight.vue'),
        redirect: '/home/orders',
        children: [
          // 右侧用户页
          {
            path: '/home/orders',
            component: () => import('@/views/rightView/order/order.vue'),
            meta: {
              name: '订单列表'
            }
          }
        ]
      },
      // 数据报表
      {
        path: 'reportmenu',
        name: 'reportmenu',
        meta: {
          name: '数据统计'
        },
        component: () => import('@/comment/HomeRight.vue'),
        redirect: '/home/reports',
        children: [
          // 右侧用户页
          {
            path: '/home/reports',
            component: () => import('@/views/rightView/report/Report.vue'),
            meta: {
              name: '数据报表'
            }
          }
        ]
      }
    ]
  }
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]

const router = new VueRouter({
  routes
})

// 全局导航守卫
router.beforeEach((to, from, next) => {
  if (to.path.indexOf('/home') !== -1) {
    // 判断是否有token
    const token = localStorage.getItem('token')
    // 有token就放行
    if (token) {
      next()
    } else {
      // 没有token就跳转到登录页
      next('/login')
    }
  } else {
  // 不是后台主页直接放行
    next()
  }
})

export default router
